package com.z.pms1.service.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.z.pms1.model.C_user;
import com.z.pms1.service.PortalService;

public class PortalServiceImpl implements PortalService {

	private EntityManager em;
	
	@PersistenceContext
	public void setEntityManager(EntityManager em) {
		this.em = em;
	}

	@SuppressWarnings("unchecked")
	public List<C_user> findAll() {
		Query query = getEntityManager().createQuery("select u FROM C_user u");
		return query.getResultList();
	}

	public void save(C_user user) {
		if (user.getUser_id() == null) {
			em.persist(user);
		} else {
			em.merge(user);
		}
	}

	public void remove(String user_id) {
		C_user user = find(user_id);
		if (user != null) {
			em.remove(user);
		}
	}

	public C_user find(String user_id) {
		return em.find(C_user.class, user_id);
	}

	private EntityManager getEntityManager() {
		return em;
	}

}
